package org.itboys.mobile.controller.mobile;



import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年2月9日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_文件上传_controller。
 * 
 */
@Controller
@RequestMapping("/sunday/mobile/upload")
public class SundayMobileUploadController extends BaseController {


	@Autowired
	private SundayUploadService uploadService;


	/**
	 * 单个文件上传
	 * @param request
	 * @param response
	 * @param file
	 */
	@RequestMapping(value = "/uploadSingle",method= RequestMethod.POST)
	@ApiOperation(value = "单个文件上传。图片上传，大小超过1MB,将会被压缩")
	public void uploadSingle(HttpServletRequest request,
								 HttpServletResponse response,
							 @ApiParam(required = true, name = "file", value = "单个文件流") @RequestParam(value="file",required=true)MultipartFile file) {
		try {

			Map<String,String> result = uploadService.uploadSingle(file);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgResultDO(result, CommonConstants.SUCCESS));
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(-1, CommonConstants.FAIL));
		}
	}



	/**
	 * 多个文件上传
	 * @param request
	 * @param response
	 * @param files
	 */
	@RequestMapping(value = "/uploadMulti",method= RequestMethod.POST)
	public void uploadMulti(HttpServletRequest request,
								 HttpServletResponse response,
							@ApiParam(required = true, name = "files", value = "多个文件流。数组")  @RequestParam(value="files",required=true)MultipartFile[] files ) {
		try {

			List<Map<String,String>> result = uploadService.uploadMulti(files);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgResultDO(result, CommonConstants.SUCCESS));
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(-1, CommonConstants.FAIL));
		}
	}

}
